<template>
  <div class="m-login-container">
    <el-row class="m-main-header-search" justify="space-between">
      <el-form hide-required-asterisk es="rules" :inline="true" ref="refForm">
        <el-row>
          <el-form-item label="选择部门">
            <el-select
              v-model="group_id"
              @change="getList"
              size="small"
              filterable
              reserve-keyword
              placeholder="请选择部门"
            >
              <el-option
                v-for="item in groupList"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              />
            </el-select>
          </el-form-item>
        </el-row>
      </el-form>
      <el-button
        @click="handleOpareteClick(1)"
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
      >
        新增
      </el-button>
    </el-row>
    <div class="m-main-content-body">
      <p class="contant-body-title">角色</p>

      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="list"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        :header-cell-style="{
          'background-color': '#E5EEFF',
        }"
      >
        <el-table-column
          prop="name"
          label="角色名"
          :autoFit="true"
          :fitHeader="true"
        >
        </el-table-column>
        <el-table-column
          prop="user_num"
          label="用户数量"
          :autoFit="true"
          :fitHeader="true"
        ></el-table-column>
        <el-table-column
          prop="time"
          label="创建时间"
          :autoFit="true"
          :fitHeader="true"
        >
          <template slot-scope="scope">
            {{
              scope.row.time
                ? $moment(scope.row.time).format("YYYY/MM/DD HH:mm:ss")
                : "--"
            }}
          </template>
        </el-table-column>
        <!-- <el-table-column
            fixed="right"
            label="操作"
            :autoFit="true" :fitHeader="true">
            <template slot-scope="scope">
              <span>
                <el-button @click="handleOpareteClick(2, scope.row)" type="text" size="small" >
                  编辑
                </el-button>
              </span>
            </template>
          </el-table-column> -->
      </el-table>
    </div>
    <dialog-set
      v-if="dislogSetVisible"
      @close="dislogSetVisible = false"
      :info="tempInfo"
      @getList="getList"
    ></dialog-set>
  </div>
</template>

<script>
import DialogSet from "./components/DialogSet";
export default {
  components: {
    DialogSet,
  },
  data() {
    return {
      list: [],
      loading: false,
      dislogSetVisible: false,
      tempInfo: {},
      group_id: "",
      groupList: [],
    };
  },
  async mounted() {
    await this.getGroupList();
    await this.getList();
  },
  methods: {
    /*
     * @Desc: 获取列表
     * @params: {String}
     * @return: {Object}
     */
    async getList() {
      this.loading = true;
      const { data } = await this.$api.role.getList({
        group_id: this.group_id,
      });
      this.__globalHandleResponseMixin(data, () => {
        this.list = data.data;
      });
      this.loading = false;
    },

    /*
     * @Desc: 编辑
     * @params: {String}
     * @return: {Object}
     */
    async handleOpareteClick(type, row = {}) {
      this.tempInfo = {
        type,
        ...row,
      };
      this.dislogSetVisible = true;
    },

    /*
     * @Desc: 获取部门列表
     * @params: {String}
     * @return: {Object}
     */
    async getGroupList() {
      this.loading = true;
      const { data } = await this.$api.group.getList();
      this.__globalHandleResponseMixin(data, () => {
        this.groupList = data.data;
        this.group_id = data.data[0].id;
      });
      this.loading = false;
    },
  },
};
</script>

<style lang="less" scoped></style>
